#include <Arduino.h>

long HX711_Buffer = 0;
long Weight_Maopi = 0,Weight_Shiwu = 0;
float Weight = 0;

#define HX711_SCK 4
#define HX711_DT 5
#define GapValue 445

void Init_Hx711();
unsigned long HX711_Read(void);
long Get_Weight();
void Get_Maopi();

unsigned long HX711_Read(void)  //增益128
{
  unsigned long count; 
  unsigned char i;
  bool Flag = 0;

  digitalWrite(HX711_DT, HIGH);
  delayMicroseconds(1);

  digitalWrite(HX711_SCK, LOW);
  delayMicroseconds(1);

    count=0; 
    while(digitalRead(HX711_DT)); 
    for(i=0;i<24;i++)
  { 
      digitalWrite(HX711_SCK, HIGH); 
    delayMicroseconds(1);
      count=count<<1; 
    digitalWrite(HX711_SCK, LOW); 
    delayMicroseconds(1);
      if(digitalRead(HX711_DT))
      count++; 
  } 
  digitalWrite(HX711_SCK, HIGH); 
  count ^= 0x800000;
  delayMicroseconds(1);
  digitalWrite(HX711_SCK, LOW); 
  delayMicroseconds(1);
  
  return(count);
}
void Init_Hx711()
{
  pinMode(HX711_SCK, OUTPUT); 
  pinMode(HX711_DT, INPUT);
}
void Get_Maopi()
{
  Weight_Maopi = HX711_Read();    
} 
long Get_Weight()
{
  HX711_Buffer = HX711_Read();
  Weight_Shiwu = HX711_Buffer;
  Weight_Shiwu = Weight_Shiwu - Weight_Maopi;       //
  Weight_Shiwu = (long)((float)Weight_Shiwu/GapValue);  
  return Weight_Shiwu;
}

void setup()
{
	Init_Hx711();				//初始化HX711模块连接的IO设置

	Serial.begin(9600);
	Serial.print("Welcome to use!\n");

	delay(3000);
	Get_Maopi();		//获取毛皮
}

void loop()
{
	Weight = Get_Weight();	//计算放在传感器上的重物重量
	Serial.print(float(Weight/1000),3);	//串口显示重量
	Serial.print(" kg\n");	//显示单位
	Serial.print("\n");		//显示单位
	delay(1000);				//延时1s
}
